/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.sys.entity.Vender;
import com.yuhanginfo.modules.sys.service.MaterielService;
import com.yuhanginfo.modules.sys.service.VenderService;

/**
 * 制造商品牌Controller
 * @author lvls
 * @version 2017-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/vender")
public class VenderController extends BaseController {

	@Autowired
	private VenderService venderService;
	
	@Autowired
	private MaterielService materielService;
	
	private static final  String VENDER = "vender";
	
	private static final  String REDIRECT = "redirect:";
	
	private static final  String LINK = "/sys/vender/?repage";
	@ModelAttribute
	public Vender get(@RequestParam(required=false) String id) {
		Vender entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = venderService.get(id);
		}
		if (entity == null){
			entity = new Vender();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = {"list", ""})
	public String list(Vender vender, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Vender> page = venderService.findPage(new Page<Vender>(request, response), vender); 
		model.addAttribute("page", page);
		return "modules/sys/vender/venderList";
	}

	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "form")
	public String form(Vender vender, Model model) {
		model.addAttribute(VENDER, vender);
		return "modules/sys/venderForm";
	}

	@RequiresPermissions("sys:vender:edit")
	@RequestMapping(value = "save")
	public String save(Vender vender, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, vender)){
			return form(vender, model);
		}
		venderService.save(vender);
		addMessage(redirectAttributes, "保存制造商品牌成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:vender:edit")
	@RequestMapping(value = "delete")
	public String delete(Vender vender, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response, Model model) {
		venderService.delete(vender);
		addMessage(redirectAttributes, "删除制造商品牌成功");
		return list(vender, request, response, model);
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "venderEdit")
	public String venderEdit(Vender vender, Model model) {
		model.addAttribute(VENDER, venderService.get(vender.getId()));
		return "modules/sys/vender/venderEdit";
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "venderEditSave")
	public String venderEditSave(Vender vender, Model model, RedirectAttributes redirectAttributes) {
		model.addAttribute(VENDER, venderService.get(vender.getId()));
		addMessage(redirectAttributes, "修改制造商品牌成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "venderAdd")
	public String venderAdd(Vender vender, Model model) {
		return "modules/sys/vender/venderAdd";
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "venderAddSave")
	public String venderAddSave(Vender vender, Model model, RedirectAttributes redirectAttributes) {
		venderService.save(vender);
		addMessage(redirectAttributes, "添加制造商品牌成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "isExistMaterielByVenderCode")
	@ResponseBody
	public int isExistMaterielByVenderCode(Vender vender, Model model, RedirectAttributes redirectAttributes) {
		return materielService.isExistMaterielByVenderId(vender.getId());
	}
	
	@RequiresPermissions("sys:vender:view")
	@RequestMapping(value = "checkSameName")
	@ResponseBody
	public String checkSameName(String oldName, String name) {
		if (name!=null && name.equals(oldName)) {
			return "true";
		} else if (name!=null && venderService.getByName(name) == null) {
			return "true";
		}
		return "false";
	}

}